﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;

namespace QuanLyShopThoiTrang
{
    public partial class TraCuuSanPham : Form
    {
        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();
        QuanLyHangHoa qlhh = new QuanLyHangHoa();

        public TraCuuSanPham()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                textBox9.Enabled = true;
            else
                textBox9.Enabled = false;
        }

        public void Set(QuanLyHangHoa q)
        {
            qlhh = q;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
                comboBox2.Enabled = true;
            else
                comboBox2.Enabled = false;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
                comboBox1.Enabled = true;
            else
                comboBox1.Enabled = false;
        }

        private void TraCuuHangHoa_Load(object sender, EventArgs e)
        {
            List<LoaiSanPham> listLoaiSP = ws.LoaiSanPham_LayDanhSach();
            comboBox2.DataSource = listLoaiSP;
            comboBox2.DisplayMember = "LoaiSanPham_Ten";

            NhaCungCap temp = new NhaCungCap();
            temp.MaNhaCungCap = -1;
            List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(temp);

            comboBox1.DataSource = listNCC;
            comboBox1.DisplayMember = "NhaCungCap_Ten";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text != "")
            {
                SanPham sp = new SanPham();
                sp.SanPham_MaHang = textBox1.Text;
                qlhh.TraCuu(sp);
                Close();
            }
            else
            {
                SanPham sp = new SanPham();
                sp.SanPham_MaHang = textBox1.Text;
                sp.SanPham_Ten = textBox5.Text;

                if (checkBox2.Checked)
                {
                    LoaiSanPham lsp = (LoaiSanPham)comboBox2.SelectedItem;
                    sp.MaLoaiSanPham = lsp.MaLoaiSanPham;
                }
                else
                    sp.MaLoaiSanPham = 0;

                if (checkBox3.Checked)
                {
                    NhaCungCap ncc = (NhaCungCap)comboBox1.SelectedItem;
                    sp.MaNhaCungCap = ncc.MaNhaCungCap;
                }
                else
                    sp.MaNhaCungCap = 0;

                sp.SanPham_NhanHieu = textBox3.Text;
                sp.SanPham_XuatXu = textBox7.Text;
                if (textBox4.Text != "")
                {
                    if(ws.KiemTraDouble(textBox4.Text))
                        sp.SanPham_GiaKhiMua = double.Parse(textBox4.Text);
                    else
                    {
                        MessageBox.Show("Giá mua không hợp lệ");
                        return;
                    }
                }
                else
                    sp.SanPham_GiaKhiMua = 0;

                if (textBox8.Text != "")
                {
                    if (ws.KiemTraDouble(textBox8.Text))
                        sp.SanPham_GiaKhiBan = double.Parse(textBox8.Text);
                    else
                    {
                        MessageBox.Show("Giá bán không hợp lệ");
                        return;
                    }
                }
                else
                    sp.SanPham_GiaKhiBan = 0;

                if (checkBox1.Checked && textBox9.Text != "")
                {
                    if (ws.KiemTraDouble(textBox9.Text))
                        sp.SanPham_GiaUuDai = double.Parse(textBox9.Text);
                    else
                    {
                        MessageBox.Show("Giá ưu đãi không hợp lệ");
                        return;
                    }
                }
                else
                    sp.SanPham_GiaUuDai = 0;

                sp.SanPham_KichCo = textBox2.Text;

                if (textBox6.Text != "")
                {
                    if(ws.KiemTraInt(textBox6.Text))
                        sp.SanPham_SoLuong = int.Parse(textBox6.Text);
                    else
                    {
                        MessageBox.Show("Số lượng không hợp lệ");
                        return;
                    }
                }
                else
                    sp.SanPham_SoLuong = -1;

                sp.SanPham_HinhAnh = "";
                qlhh.TraCuu(sp);
                Close();
            }
        }
    }
}
